<?php
class ProjectReward extends AppModel
{
    public $name = 'ProjectReward';
    //$validate set in __construct for multi-language support
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    public $belongsTo = array(
        'Project' => array(
            'className' => 'Project',
            'foreignKey' => 'project_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
        )
    );
    public $hasMany = array(
        'ProjectFund' => array(
            'className' => 'ProjectFund',
            'foreignKey' => 'project_reward_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
    function __construct($id = false, $table = null, $ds = null)
    {
        parent::__construct($id, $table, $ds);
        $this->validate = array(
            'pledge_amount' => array(
                'rule6' => array(
                    'rule' => '_rewardNotEmpty',
                    'allowEmpty' => (Configure::read('Project.is_project_reward_optional')) ? true : false,
                    //'message' => '',
                    
                ) ,
                'rule5' => array(
                    'rule' => '_rewardAmountCheck',
                    'allowEmpty' => (Configure::read('Project.is_project_reward_optional')) ? true : false,
                    //  'message' => ''
                    
                ) ,
                'rule4' => array(
                    'rule' => '_rewardAmount',
                    'allowEmpty' => (Configure::read('Project.is_project_reward_optional')) ? true : false,
                    'message' => __l('Must be less than needed amount')
                ) ,
                'rule3' => array(
                    'rule' => array(
                        'comparison',
                        '>=',
                        1
                    ) ,
                    'allowEmpty' => (Configure::read('Project.is_project_reward_optional')) ? true : false,
                    'message' => __l('Must be greater than zero')
                ) ,
                'rule2' => array(
                    'rule' => 'numeric',
                    'allowEmpty' => (Configure::read('Project.is_project_reward_optional')) ? true : false,
                    'message' => __l('Enter valid amount')
                ) ,
                'rule1' => array(
                    'rule' => 'notempty',
                    'allowEmpty' => (Configure::read('Project.is_project_reward_optional')) ? true : false,
                    'message' => __l('Required')
                ) ,
            ) ,
            'reward' => array(
                'rule2' => array(
                    'rule' => '_pledgeNotEmpty',
                    'allowEmpty' => (Configure::read('Project.is_project_reward_optional')) ? true : false,
                    'message' => '',
                ) ,
                'rule1' => array(
                    'rule' => 'notempty',
                    'allowEmpty' => (Configure::read('Project.is_project_reward_optional')) ? true : false,
                    'message' => __l('Required')
                ) ,
            ) ,
            'pledge_max_user_limit' => array(
                'rule2' => array(
                    'rule' => '_checkUserCount',
                    'allowEmpty' => true,
                    'message' => __l('This limit can not be entered  ')
                ) ,
                'rule1' => array(
                    'rule' => 'numeric',
                    'allowEmpty' => true,
                    'message' => __l('Enter valid limit in numbers')
                ) ,
            ) ,
        );
    }
    function _checkUserCount()
    {
        if ($this->data[$this->name]['pledge_amount'] * $this->data[$this->name]['pledge_max_user_limit'] > $this->data[$this->name]['max_amount'] && !$this->data[$this->name]['is_allow_over_funding']) {
            return false;
        } else {
            return true;
        }
    }
    function _rewardAmountCheck()
    {
        if ($this->data[$this->name]['pledge_amount'] != $this->data[$this->name]['min_amount'] && $this->data[$this->name]['pledge_type_id'] == ConstPledgeTypes::Fixed) {
            $this->validationErrors['pledge_amount'] = __l('Amount should be equal to fixed amount');
            return true;
        } else if ($this->data[$this->name]['pledge_amount'] < $this->data[$this->name]['min_amount'] && $this->data[$this->name]['pledge_type_id'] == ConstPledgeTypes::Minimum) {
            $this->validationErrors['pledge_amount'] = __l('Amount should not be less then minimum amount');
            return true;
        } else if ($this->data[$this->name]['pledge_amount'] < $this->data[$this->name]['min_amount'] && $this->data[$this->name]['pledge_type_id'] == ConstPledgeTypes::Multiple) {
            $this->validationErrors['pledge_amount'] = __l('Amount should not be less then denomination amount');
            return true;
        } else if ($this->data[$this->name]['max_amount'] % $this->data[$this->name]['pledge_amount'] != 0 && $this->data[$this->name]['pledge_type_id'] == ConstPledgeTypes::Multiple && !$this->data[$this->name]['is_allow_over_funding']) {
            $this->validationErrors['pledge_amount'] = __l('Amount cannot be equally shared or else you should allow over funding.');
            return true;
        } else {
            return true;
        }
    }
    function _rewardAmount()
    {
        if ($this->data[$this->name]['pledge_amount'] >= $this->data[$this->name]['max_amount']) {
            return false;
        } else {
            return true;
        }
    }
    function _rewardNotEmpty()
    {
        if (!empty($this->data[$this->name]['pledge_amount']) && empty($this->data[$this->name]['reward']) && empty($this->validationErrors['reward'])) {
            $this->validationErrors['reward'] = __l('Required');
            return true;
        } else {
            return true;
        }
    }
    function _pledgeNotEmpty()
    {
        if (empty($this->data[$this->name]['pledge_amount']) && empty($this->validationErrors['pledge_amount'])) {
            $this->validationErrors['pledge_amount'] = __l('Required');
            return false;
        } else {
            return true;
        }
    }
}
?>